/*
package com.itheima.servlet;

import com.itheima.domian.Studnet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class AddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf8");
        Map<String, String[]> parameterMap = req.getParameterMap();
        Studnet studnet=new Studnet();

        try {
            BeanUtils.populate(studnet,parameterMap);
            if(studnet.getId()==null||studnet.getId().equals("")){
                studnet.setId(UUID.randomUUID().toString());
                String realPath = req.getServletContext().getRealPath("studnet.txt");
                System.out.println("realPath"+realPath);
                BufferedReader bufferedReader =new BufferedReader(new FileReader(realPath));
                OutputStreamWriter outputStreamWriter =new OutputStreamWriter(new FileOutputStream(realPath ,true),"utf-8");
                BufferedWriter bufferedWriter=new BufferedWriter(outputStreamWriter);
                bufferedWriter.write(studnet.toString());
                bufferedWriter.newLine();
                bufferedWriter.close();

            }else{
                String realPath = req.getServletContext().getRealPath("/student.txt");
                System.out.println("realPath"+realPath);
                InputStreamReader inputStreamReader =new InputStreamReader(new FileInputStream(realPath),"utf-8");
                BufferedReader bufferedReader =new BufferedReader(inputStreamReader);
                String str="";
                List<Studnet>studnets=new ArrayList<>();
                while ((str=bufferedReader.readLine())!=null){
                    if (str.startsWith(studnet.getId())){
                        studnets.add(studnet);
                        continue;
                    }
                    String[] split = str.split(",");
                    studnets.add(new Studnet(split[0],split[1],Integer.parseInt(split[2])));

                }
                bufferedReader.close();
                inputStreamReader.close();
                OutputStreamWriter outputStreamWriter =new OutputStreamWriter(new FileOutputStream(realPath),"utf-8");
                BufferedWriter bufferedWriter =new BufferedWriter(outputStreamWriter);
                for (Studnet sty : studnets){
                    bufferedWriter.write(sty.toString());
                    bufferedWriter.newLine();

                }
                bufferedWriter.close();
                outputStreamWriter.close();
            }
            resp.sendRedirect("/listServlet");




        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
*/
